/**
 * Copyright (C) 2022-2023
 * All rights reserved, Designed By www.tonetn.com
 * 注意：
 * 本软件为www.tonetn.com开发研制，未经购买不得使用
 * 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 * 一经发现盗用、分享等行为，将追究法律责任，后果自负
 */
package com.xunyin.modules.recharge.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.xunyin.common.service.impl.BaseServiceImpl;
import com.xunyin.common.utils.QueryHelpPlus;
import com.xunyin.dozer.service.IGenerator;
import com.xunyin.modules.recharge.domain.TRechargeSet;
import com.xunyin.modules.recharge.service.TRechargeSetService;
import com.xunyin.modules.recharge.service.dto.TRechargeSetDto;
import com.xunyin.modules.recharge.service.dto.TRechargeSetQueryCriteria;
import com.xunyin.modules.recharge.service.mapper.TRechargeSetMapper;
import com.xunyin.utils.FileUtil;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


/**
 * @Author xunyinyunshang  <3594419785@qq.com>
 * @date 2022-10-12
 */
@Service
@AllArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class TRechargeSetServiceImpl extends BaseServiceImpl<TRechargeSetMapper, TRechargeSet> implements TRechargeSetService {

    private final IGenerator generator;

    @Autowired
    private TRechargeSetMapper tRechargeSetMapper;

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(TRechargeSetQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<TRechargeSet> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", page.getList());
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<TRechargeSet> queryAll(TRechargeSetQueryCriteria criteria) {
        return baseMapper.selectList(QueryHelpPlus.getPredicate(TRechargeSet.class, criteria));
    }

    @Override
    public List<TRechargeSet> getRechargeOptionlist(Integer teaHouseId, String userPhone, Integer mainId) {
        return tRechargeSetMapper.selectByTeaHouseIdAndPhone(teaHouseId, userPhone, mainId);
    }

    /**
     * 根据美团团购项目Id,获取套餐信息
     * @param dealId
     * @return
     */
    @Override
    public TRechargeSet selectBydealId(Integer dealId) {
        LambdaQueryWrapper<TRechargeSet> wrapper = new LambdaQueryWrapper<>();
        wrapper.and(i -> i.eq(TRechargeSet::getDealId, dealId));
        return tRechargeSetMapper.selectOne(wrapper);
    }


    @Override
    public void download(List<TRechargeSetDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TRechargeSetDto tRechargeSet : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("优惠套餐名称", tRechargeSet.getName());
            map.put("价格（元）", tRechargeSet.getPrice());
            map.put("时长（小时）", tRechargeSet.getTimeLong());
            map.put("会员等级", tRechargeSet.getLevel());
            map.put("主体Id", tRechargeSet.getMainId());
            map.put("会客厅ID（门店Id）", tRechargeSet.getTeaHouseId());
            map.put("会客厅名称（门店名称）", tRechargeSet.getTeaHouseName());
            map.put("转让次数", tRechargeSet.getTransferTimes());
            map.put("标识图片", tRechargeSet.getImage());
            map.put("有效开始时间", tRechargeSet.getValidityDateStart());
            map.put("有效结束时间", tRechargeSet.getValidityDateEnd());
            map.put("是否可用", tRechargeSet.getEnable());
            map.put("状态", tRechargeSet.getStatus());
            map.put("限购1次标志", tRechargeSet.getPurchaseLimitsOneFlag());
            map.put(" activityNo", tRechargeSet.getActivityNo());
            map.put(" activityPurchaseLimitTimes", tRechargeSet.getActivityPurchaseLimitTimes());
            map.put("主体名称", tRechargeSet.getMainName());
            map.put("创建日期", tRechargeSet.getCreateTime());
            map.put("更新日期", tRechargeSet.getUpdateTime());
            map.put("删除日期", tRechargeSet.getDeleteTime());
            map.put(" isDel", tRechargeSet.getIsDel());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
